package com.chiry.collectionstream.demos.date;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalField;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author chiryvan
 * @Date 2022/7/20
 **/
@RestController
public class TestDate {
    @RequestMapping("date")
    public String test1(){
        Date date = new Date();
        long time = date.getTime();
        System.out.println("time = " + time);//时间戳
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS'Z'");
        String format = simpleDateFormat.format(date);
        System.out.println("date.toString() = " + date.toString());
        return format;
    }
    @RequestMapping("date2")
    public String test2(){
        Date date = new Date();
        Map<Object, Object> maps = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            maps.put(i, new Date());
        }
        System.out.println("maps = " + maps);
        List<Object> keys = maps.entrySet().stream().map(Map.Entry::getKey).collect(Collectors.toList());
        System.out.println("keys = " + keys);
        maps.entrySet().stream().forEach(e -> e.setValue(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS'Z'").format(date)));
        System.out.println("maps = " + maps);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS'Z'");
        String format = simpleDateFormat.format(date);
        return format;
    }
    @RequestMapping("date3")
    public String test3(){
        //date
        LocalDate now = LocalDate.now();
        String format = now.format(DateTimeFormatter.ofPattern("yyy-MM-dd"));
        System.out.println("format = " + format);
        LocalDateTime nowTime = LocalDateTime.now();
        String datetime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss:SSS'Z'"));
        System.out.println("datetime = " + datetime);
        //LocalDate -->Date
        Date date = new Date(nowTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
        System.out.println("date = " + date);
        return format;
    }
}
